#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
	int score = 0;
	float width = 600, height = 400;
	initgraph(width, height);
	float ball_x, ball_y, radius = 10, gravity, rect_height=100, rect_width = 20, rect_left_x, rect_left_y1 = height - rect_height, vy, rect_vx = -3, rect_left_y2=50;
	ball_x = width / 4;
	ball_y = height/2;
	gravity = 0.2;
	vy = 0;
	rect_left_x = width * 3 / 4;
	while (1)
	{
		if (_kbhit())
		{
			char input = _getch();
			if (input == ' ')
			{
				vy = -4;
				
			}
		}vy = vy + gravity;
		ball_y = ball_y + vy;
		if (ball_y >= height - radius)
		{
			ball_y = height - radius;
			vy = 0;
		}
		if (ball_y <= radius)
			ball_y = radius;
		rect_left_x = rect_left_x + rect_vx;
		
		if (rect_left_x <= 0)
		{
			score = score + 1;
			rect_left_x = width;
			rect_height = rand() % int(height / 4)+height/4 ;
			rect_vx = rand() / float(RAND_MAX) * 4 - 7;
			rect_left_y1 = height - rect_height;
			rect_left_y2 = rand() % int(height /4) + height / 4;
		}
		if ((ball_x + radius >= rect_left_x)
			&& (ball_x - radius <= rect_left_x +rect_width)
			&& (ball_y + radius >= height - rect_height))
		{
			score = 0;
			Sleep(20);

		}
		if ((ball_x + radius >= rect_left_x) && (ball_x - radius <= rect_left_x + rect_width) && (ball_y - radius <= rect_left_y2))
		{
			score = 0;
			Sleep(20);
		}
		cleardevice();
		fillcircle(ball_x, ball_y, radius);
		fillrectangle(rect_left_x, rect_left_y1, rect_left_x + rect_width, height);
		fillrectangle(rect_left_x,rect_left_y2, rect_left_x + rect_width, 0);
		TCHAR s[20];
		swprintf_s(s, _T("%d"), score);
		settextstyle(40, 0, _T("宋体"));
		outtextxy(50, 30, s);
		Sleep(10);
	}
	closegraph();
	return 0;
}